import { createRouter, createWebHistory } from 'vue-router'

import Login from '../pages/login/index.vue'
import Home from '../pages/home/index.vue'
import Dialog from '../pages/home/Dialog.vue'
import newOrder from '../pages/home/newOrder.vue'
import CustomerList from '../pages/customer/customerList.vue'
import CustomerEdit from '../pages/customer/customerEdit.vue'
import GoodsEdit from '../pages/goods/goodsEdit.vue'
import GoodsList from '../pages/goods/goodsList.vue'
import Register from '../pages/register/index.vue'
import store from '../store'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {path: '/', redirect: '/login'},
        {path: '/login', component: Login, meta: {nologin: true}},
        {path: '/home', component: Home},
        {path: '/home/Dialog', component: Dialog},
        {path: '/home/newOrder', component: newOrder},
        {path: '/register', component: Register,meta:{noRegister:true}},
        {path: '/customer/list', component: CustomerList},
        {path: '/customer/edit', component: CustomerEdit},
        {path: '/goods/list', component: GoodsList},
        {path: '/goods/edit', component: GoodsEdit},
    ]
})

router.beforeEach(function(to, _, next) {
    if (to.meta.nologin || to.meta.noRegister) {
        next()
    } else {
        /* 因为登录以后会存储token到vuex，所以可以获取token数据 */
        let token = store.getters['common/token']
        if (!token) {
            next('/login')
        } else {
            next()
        }
    }
})

export default router